import {req} from "./request";

export interface Draft {
    draft_id: number
    user_version: string
    user_desc: string
    create_time: number
    source_miniprogram_appid: string
    source_miniprogram: string
    developer: string
}

export interface Template {
    template_id: number
    user_version: string
    draft_id: number
    user_desc: string
    create_time: number
    source_miniprogram_appid: string
    source_miniprogram: string
    developer: string
    template_type: string
}

export interface mp {
    appid: string
    app_name: string
    preview_version: string
    release_version: string
    audit_status: number
    audit_id: number
}

class Api {


    // 获取到草稿箱
    public async getDraftList() {
        const data = await req.post('code/draftList')
        if (!data?.list?.length) {
            throw new Error(`草稿箱中没有内容: ${JSON.stringify(data)}`)
        }
        return data.list.reduce((a: Draft, b: Draft) => a.create_time > b.create_time ? a : b);
    }

    // 提交到模板库 /code/addToTemplate
    public async addToTemplate(draft_id: number) {
        const form = new FormData();
        form.append('draft_id', `${draft_id}`);
        await req.post('code/addToTemplate', form)
    }

    // 获取模板库列表
    public async getTemplateList() {
        const data = await req.post('code/templateList')
        if (!data?.list?.length) {
            throw new Error(`模板库中没有内容: ${JSON.stringify(data)}`)
        }
        return data.list.reduce((a: Template, b: Template) => a.template_id > b.template_id ? a : b);
    }

    // 获取授权小程序列表
    public async getMiniAppList() {
        const form = new FormData();
        form.append('template_type', '0');
        const data = await req.post('mp/miniAppList', form)
        if (!data?.list?.length) {
            throw new Error(`没有授权的小程序: ${JSON.stringify(data)}`)
        }
        return data.list as mp[];
    }

    // 提交到体验版 /mp/commitTemplate
    public async commitTemplate(appid: string, template_id: number, version: string, desc: string) {
        const form = new FormData();
        form.append('appid', appid);
        form.append('template_id', `${template_id}`);
        form.append('version', version);
        form.append('desc', desc);
        await req.post('mp/commitTemplate', form)
    }

    // 获取体验版二维码地址 /mp/getQrCode
    public async getQrCode(appid: string) {
        const form = new FormData();
        form.append('appid', appid);
        const data = await req.post('mp/getQrCode', form)
        return data.url as string;
    }

    // 提审 /mp/submitAudit
    async submitAudit(appid: string, desc: string) {
        console.log("提审", appid, desc)
        if (!appid) {
            return
        }
        const form = new FormData();
        form.append('appid', appid);
        form.append('desc', desc);
        await req.post('mp/submitAudit', form);

    }

    // 发布 /mp/releaseMiniApp
    async releaseMiniApp(appid: string) {
        console.log("发布", appid)
        if (!appid) {
            return
        }
        const form = new FormData();
        form.append('appid', appid);
        await req.post('mp/releaseMiniApp', form);
    }
}

export const api = new Api()
